import {scrapingUtils} from './scrapingUtils.js'


export class scrapingTools
{
    constructor(page, filterTools, highlightsTools)
    {
        this.page = page ;
        this.utils = scrapingUtils ;
        this.highlightsTools = highlightsTools;
        this.filterTools = filterTools ;
        this.scrapedData = [] ;
    }

    async scrapeWithSelector(selector)
    {
        const elements = await this.page.ref.evaluate(({selector,extractionStr, visibilityCheckerStr}) =>
        {   
            const extract = eval(extractionStr) ;
            const check = eval(visibilityCheckerStr) ;
            return Array.from(document.querySelectorAll(selector))
            .filter(check)
            .map(element => extract(element)) ;
        }, 
        {
            selector:selector,
            extractionStr: this.utils.getExtractDataFromElementsFunctionString(),
            visibilityCheckerStr: this.utils.getCheckIfVisibleFunctionString()
        });
        const ans = await this.filterTools.filterScrapedElements(elements) ;
        return ans;
    }

    async scrape({}, deleteBoundingBoxes = true, scrapeEverything = false)
    {
        let selector = this.utils.getReducedSelector() ;
        if(scrapeEverything) selector = this.utils.getEverythingSelector() ;
    
        let results = await this.scrapeWithSelector(selector);

        for(let i = 0; i< results.length; i++) results[i].index = i ;
    
        this.highlightsTools.highlightElements(results) ;
    
        this.scrapedData = structuredClone(results) ;
        if(deleteBoundingBoxes) results.forEach(el => {delete el.boundingBox;});
    
        return results;
    }

    getScrapedData(index)
    {
        return this.scrapedData[index] ;
    }
}